package com.demo.crud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: keen
 * Date: 2019-06-07
 * Time: 15:55
 */
@Controller
public class LoginController {
   /* @Value(value = "${test.config}")
    private String testConfig;*/

    public LoginController(@Value("${test.config}") String testConfig){
        System.out.println(testConfig);
    }
    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String, String> errorMap, HttpSession session) {
        if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
            session.setAttribute("userName", username);
            return "redirect:main.html";
        } else {
            errorMap.put("msg", "用户名或密码错误");
            return "index";
        }
       /* ModelAndView mv = new ModelAndView();
        if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
            mv.setViewName("dashboard");
        } else {
            errorMap.put("msg", "用户名或密码错误");
            mv.setViewName("index");
        }
        return mv;*/
    }
}
